package com.hl.springcloud.controller;

import com.hl.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author Liu Huan
 */

@RestController
public class DeptConsumerController {
	
	//消费者 不应该有 service 层
	//可以通过 http 访问 服务提供者的 controller
	//springboot 有 restTemplete 调用 http 请求
	@Autowired
	private RestTemplate restTemplate;
	
	//由于请求的地址固定
	//private static final String REST_URL_PREFIX = "http://localhost:8001";
	//通过 ribbon 负载均衡算法访问三个注册中心上的 同一个微服务
	private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";
	
	// http://localhost:8001/dept/list
	@RequestMapping("/consumer/dept/get/{id}")
	public Dept get(@PathVariable("id") Long id){
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id, Dept.class);
	}
	
	@RequestMapping("/consumer/dept/add")
	public boolean addDept(Dept dept){
		return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add/", dept, Boolean.class);
	}
	
	@RequestMapping("/consumer/dept/get/list")
	public List<Dept> list(){
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/list", List.class);
	}
}
